home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / ROM_Kernel_Manuals / Lib_examples / bob.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  5.8 KB  |  165 lines

  1. /* This example must be linked with animtools.c and includes the header
  2. ** files animtools.h and animtools_proto.h.  These files are listed at
  3. ** the end of the chapter.
  4. **
  5. ** bob.c
  6. **
  7. ** SAS/C V5.10a
  8. ** lc -b1 -cfist -v -y bob.c
  9. ** blink FROM LIB:c.o bob.o animtools.o LIB LIB:lc.lib LIB:amiga.lib TO bob
  10. */
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <intuition/intuitionbase.h>
  14. #include <graphics/gfx.h>
  15. #include <graphics/gfxbase.h>
  16. #include <graphics/gels.h>
  17. #include <libraries/dos.h>
  18. #include <stdlib.h>
  19. #include "animtools.h"
  20.  
  21. VOID bobDrawGList(struct RastPort *rport, struct ViewPort *vport);
  22. VOID process_window(struct Window *win, struct Bob *myBob);
  23. VOID do_Bob(struct Window *win);
  24.  
  25. struct GfxBase       *GfxBase;       /* pointer to Graphics library */
  26. struct IntuitionBase *IntuitionBase; /* pointer to Intuition library*/
  27. int return_code;
  28. #define GEL_SIZE  4                  /* number of lines in the bob  */
  29.  
  30. /* Bob data - two sets that are alternated between.  Note that this */
  31. /* data is at the resolution of the screen.                         */
  32.  
  33. /* data is 2 planes by 2 words by GEL_SIZE lines                    */
  34. WORD chip bob_data1[2 * 2 * GEL_SIZE] =
  35.         {
  36.         /* plane 1 */
  37.         0xffff, 0x0003, 0xfff0, 0x0003, 0xfff0, 0x0003, 0xffff, 0x0003,
  38.         /* plane 2 */
  39.         0x3fff, 0xfffc, 0x3ff0, 0x0ffc, 0x3ff0, 0x0ffc, 0x3fff, 0xfffc
  40.         };
  41.  
  42. /* data is 2 planes by 2 words by GEL_SIZE lines                     */
  43. WORD chip bob_data2[2 * 2 * GEL_SIZE] =
  44.         {
  45.         /* plane 1 */
  46.         0xc000, 0xffff, 0xc000, 0x0fff, 0xc000, 0x0fff, 0xc000, 0xffff,
  47.         /* plane 2 */
  48.         0x3fff, 0xfffc, 0x3ff0, 0x0ffc, 0x3ff0, 0x0ffc, 0x3fff, 0xfffc
  49.         };
  50.  
  51. NEWBOB myNewBob =                     /* Data for the new bob structure defined in animtools.h */
  52.         {                             /* Initial image, WORD width, line height                */
  53.         bob_data2, 2,  GEL_SIZE,      /* Image depth, plane pick, plane on off, VSprite flags  */
  54.         2, 3, 0, SAVEBACK | OVERLAY,  /* dbuf (0=false), raster depth, x,y position, hit mask, */
  55.         0, 2, 160, 100,  0,0,         /* me mask                                               */
  56.         };
  57.  
  58. struct NewWindow myNewWindow =
  59.         {                             /* information for the new window */
  60.         80, 20, 400, 150, -1, -1, CLOSEWINDOW | INTUITICKS,
  61.         ACTIVATE | WINDOWCLOSE | WINDOWDEPTH | RMBTRAP,
  62.         NULL, NULL, "Bob", NULL, NULL, 0, 0, 0, 0, WBENCHSCREEN
  63.         };
  64.  
  65. /* Draw the Bobs into the RastPort. */
  66. VOID bobDrawGList(struct RastPort *rport, struct ViewPort *vport)
  67. {
  68. SortGList(rport);
  69. DrawGList(rport, vport);
  70. /* If the GelsList includes true VSprites, MrgCop() and LoadView() here */
  71. WaitTOF() ;
  72. }
  73.  
  74. /* Process window and dynamically change bob: Get messages. Go away on CLOSEWINDOW.
  75. ** Update and redisplay bob on INTUITICKS. Wait for more messages.
  76. */
  77. VOID process_window(struct Window *win, struct Bob *myBob)
  78. {
  79. struct IntuiMessage *msg;
  80.  
  81. FOREVER {
  82.         Wait(1L << win->UserPort->mp_SigBit);
  83.         while (NULL != (msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
  84.                 {
  85.                 /* only CLOSEWINDOW and INTUITICKS are active */
  86.                 if (msg->Class == CLOSEWINDOW)
  87.                         {
  88.                         ReplyMsg((struct Message *)msg);
  89.                         return;
  90.                         }
  91.                 /* Must be INTUITICKS:  change x and y values on the fly.  Note:
  92.                 ** do not have to add window offset, Bob is relative to the
  93.                 ** window (sprite relative to screen).
  94.                 */
  95.                 myBob->BobVSprite->X = msg->MouseX + 20;
  96.                 myBob->BobVSprite->Y = msg->MouseY + 1;
  97.                 ReplyMsg((struct Message *)msg);
  98.                 }
  99.         /* after getting a message, change image data on the fly */
  100.         myBob->BobVSprite->ImageData =
  101.                 (myBob->BobVSprite->ImageData == bob_data1) ? bob_data2 : bob_data1;
  102.         InitMasks(myBob->BobVSprite); /* set up masks for new image */
  103.         bobDrawGList(win->RPort, ViewPortAddress(win));
  104.         }
  105. }
  106.  
  107.  
  108. /* Working with the Bob: setup the GEL system, and get a new Bob (makeBob()).
  109. ** Add the bob to the system and display. Use the Bob.  When done, remove
  110. ** the Bob and update the display without the bob. Cleanup everything.
  111. */
  112. VOID do_Bob(struct Window *win)
  113. {
  114. struct Bob         *myBob;
  115. struct GelsInfo    *my_ginfo;
  116.  
  117. if (NULL == (my_ginfo = setupGelSys(win->RPort, 0x03)))
  118.         return_code = RETURN_WARN;
  119. else
  120.         {
  121.         if (NULL == (myBob = makeBob(&myNewBob)))
  122.                 return_code = RETURN_WARN;
  123.         else
  124.                 {
  125.                 AddBob(myBob, win->RPort);
  126.                 bobDrawGList(win->RPort, ViewPortAddress(win));
  127.                 process_window(win, myBob);
  128.                 RemBob(myBob);
  129.                 bobDrawGList(win->RPort, ViewPortAddress(win));
  130.                 freeBob(myBob, myNewBob.nb_RasDepth);
  131.                 }
  132.         cleanupGelSys(my_ginfo,win->RPort);
  133.         }
  134. }
  135.  
  136.  
  137. /* Example bob program: First open up the libraries and a window. */
  138. VOID main(int argc, char **argv)
  139. {
  140. struct Window      *win;
  141.  
  142. return_code = RETURN_OK;
  143.  
  144. if (NULL == (GfxBase = (struct GfxBase *)OpenLibrary(GRAPHICSNAME,37L)))
  145.         return_code = RETURN_FAIL;
  146. else
  147.         {
  148.         if (NULL == (IntuitionBase = (struct IntuitionBase *)OpenLibrary(INTUITIONNAME,37L)))
  149.                 return_code = RETURN_FAIL;
  150.         else
  151.                 {
  152.                 if (NULL == (win = OpenWindow(&myNewWindow)))
  153.                         return_code = RETURN_FAIL;
  154.                 else
  155.                         {
  156.                         do_Bob(win);
  157.                         CloseWindow(win);
  158.                         }
  159.                 CloseLibrary((struct Library *)IntuitionBase);
  160.                 }
  161.         CloseLibrary((struct Library *)GfxBase);
  162.         }
  163. exit(return_code);
  164. }
  165.